//
//  MyFirstSectionCell.swift
//  todaySwift
//
//  Created by ChokWah on 2018/3/13.
//  Copyright © 2018年 ChokWah. All rights reserved.
//

import UIKit

//protocol MyFirstSectionCellDelegate: class {
//    // 点击第几个cell，回调事件
//    func MyFirstSectionCell(_ firstCell: MyFirstSectionCell, myConcern: MyConcern)
//}

class MyFirstSectionCell: UITableViewCell, RegisterCellOrNib {
    /// 点击了第几个 cell
    var myConcernSelected: ((_ myConcern: MyConcern)->())?
    // 主标题
    @IBOutlet weak var leftLabel: UILabel!
    // 副标题
    @IBOutlet weak var rightLabel: UILabel!
    // 右边箭头
    @IBOutlet weak var rightImageView: UIImageView!
    @IBOutlet weak var conectionView: UICollectionView!
    
//    weak var delagate: MyFirstSectionCellDelegate?
    
    var myConcerns = [MyConcern]() { // 有多少个关注用户
        didSet {
             conectionView.reloadData()
        }
    }
    var myCellModel : MyCellModel? {
        didSet {
            leftLabel.text = myCellModel?.text
            rightLabel.text = myCellModel?.grey_text
        }
    }
    
    var myConcern : MyConcern? {
        didSet {
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        conectionView.collectionViewLayout = MyConcernFlowLayout()
        conectionView.delegate = self
        conectionView.dataSource = self
        conectionView.cwc_registerCell(cell: MyConcernCell.self)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

extension MyFirstSectionCell : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return myConcerns.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.cwc_dequeueReusableCell(indexPath: indexPath) as MyConcernCell
        cell.myConcern = myConcerns[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        myConcernSelected?(myConcerns[indexPath.item])
    }
    
    
}

class MyConcernFlowLayout: UICollectionViewFlowLayout {
    override func prepare() { // 布局信息
         itemSize = CGSize(width: 58, height: 74) // 每个Cell的大小
         minimumLineSpacing = 0 // 横向间距
         minimumInteritemSpacing = 0 // 竖向
         sectionInset = UIEdgeInsetsMake(0, 0, 0, 0) // cell距离view上下左右
        scrollDirection = .horizontal
    }
}
